package com.shs.com.controller;

import com.shs.com.entity.BookSub;
import com.shs.com.entity.ResponseBody;
import com.shs.com.service.BookService;
import com.shs.com.utils.AssembleResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BookController {
    AssembleResponseMsg responseMsg = new AssembleResponseMsg();
    @Autowired
    private BookService bookService;
    @RequestMapping("/queryBookList")
    public ResponseBody queryUserList(@RequestBody Map<String, Object> map) {

        //调用service方法
        Map<String, Object> stringObjectMap = bookService.queryBookList(map);
//        Map<String,Object> result = new HashMap<>();
//        result.put("rows",stringObjectMap.get("rows"));

        return responseMsg.success(stringObjectMap);
    }
    //添加图书
    @RequestMapping("/addBook")
    public ResponseBody addBook(@RequestBody Map<String, Object> map) {
        try {
            bookService.addBook(map);
            return responseMsg.success("OK");
        } catch (Exception e) {
            return responseMsg.failure(200, "111", "添加失败");
        }
    }
//    编辑图书
    @RequestMapping("/editBook")
    public ResponseBody editBook(@RequestBody Map<String, Object> map) {
        try {
            bookService.editBook(map);
            return responseMsg.success("OK");
        } catch (Exception e) {
            e.printStackTrace();
            return responseMsg.failure(200, "111", "编辑失败");
        }
    }
//    删除图书
    @RequestMapping("/delBook")
    public ResponseBody delBook(@RequestBody Map<String, Object> map) {
        try {
            bookService.delBook(map);
            return responseMsg.success("OK");
        } catch (Exception e) {
            return responseMsg.failure(200, "111", "删除失败");
        }
    }
    //更新图书库存
    @RequestMapping("/updateInventtories")
    public ResponseBody updateInventtories(@RequestBody Map<String, Object> map) {
        try {
            bookService.updateInventtories(map);
            return responseMsg.success("OK");
        } catch (Exception e) {
            return responseMsg.failure(200, "111", "更新库存失败");
        }
    }
    // 根据图书ID获取图书信息
    @RequestMapping("/getBookById")
    public ResponseBody getBookById(@RequestBody int id) {
        try {
            bookService.getBookById(id);
            return responseMsg.success("OK");
        } catch (Exception e) {
            return responseMsg.failure(200, "111", "获取失败");
        }
    }
    //查看借阅图书列表
    @RequestMapping("/querySub")
    public ResponseBody querySub(@RequestBody Map<String, Object> map) {
        //调用service方法
        List<BookSub> bookSubs = bookService.querySub(map);
        //传入需要返回的参数
        Map<String,Object> result = new HashMap<>();
        result.put("rows",bookSubs);
        return responseMsg.success(result);
    }

    //借阅图书
    @RequestMapping("/addSubBook")
    public ResponseBody addSubBook(@RequestBody Map<String, Object> map) {
        try {
            bookService.addSubBook(map);
            return responseMsg.success("OK");
        } catch (Exception e) {
            return responseMsg.failure(200, "111", "借阅图书失败");
        }
    }
//    归还图书
    @RequestMapping("/returnSubBook")
    public ResponseBody returnSubBook(@RequestBody Map<String, Object> map) {
        try {
            bookService.returnSubBook(map);
            return responseMsg.success("OK");
        } catch (Exception e) {
            return responseMsg.failure(200, "111", "归还图书失败");
        }
    }

}
